<?php

namespace app\blog\model;
use think\Model;
use think\Db;

class BrandModel extends Model{
    //显示品牌列表
    public function __construct($data = [])
    {
        parent::__construct($data);
        $id=cmf_get_current_admin_id();
        $this->id=$id;
    }

    public  function session()
    {
        $id=cmf_get_current_admin_id();
        $cause['id']=$id;
        $user=DB::name('user')->where($cause)->find();
        if($user['user_type']==1)
        {
            $where='';
            $session=DB::name('shop')->where($where)->select();
        }else{
            $where['re_guid']=$user['shop_guid'];
            $session=DB::name('shop')->where($where)->select()->toArray();
        }
        return $session;
    }
    public function index($param){
        $session=$this->session();
        if($session)
        {
            if($this->id == 1)
            {
                $where="";
                $shop_id="";
            }else{
                $where['b.shop_id']=$session[0]['re_guid'];
                $shop_id=$session[0]['re_guid'];
            }
            if($param)
            {
                $where['b.name']=array('like','%'.$param.'%');
            }
            $data = DB::name("brand")->alias('b')->join('shop s','s.re_guid =b.shop_id  ')->where($where)->field('b.*,s.shop_name')->paginate(1);
            if($data->toArray()) {
                foreach ($data as $k => $v) {
                    $data->toArray();
                    $date = array();
                    $date = $v;
                    if ($v['status'] == 1) {
                        $date['status_name'] = '启用';
                    } else {
                        $date['status_name'] = '禁用';
                    }
                    $data->offsetSet($k, $date);
                }
            }
            $data->appends(['name'=>$param,'shop_id'=>$shop_id]);
            $array['data']=$data;
            $array['page']=$data->render();
        }else{
            $array['data']="";
            $array['page']="";
        }
        return $array;
    }
    /*
     * 添加品牌
     * guid()生成唯一标识 在common
     * $data包含(name,status)
     */
    public function add($data){
        if(isset($data['shop']))
        {
            $log = array();
            $log['guid'] = guid();
            $log['name'] = $data['name'];
            $log['status'] =1;
            $log['shop_id'] =$data['shop'];
            $log['ctime'] = time();
            $result = Db::name('brand')->insert($log);
        }else{
            $result=0;
        }
        return $result;
    }
    /*
     * 修改品牌
     * $data包含(name)
     */
    public function goodspecupdate($where,$data){
        $result = Db::name("brand")->where($where)->update($data);
        return $result;
    }
    /*
     * 删除品牌
     * $data包含(id)
     */
    public function del($cause){
        $where['shop_id']=$cause['shop_id'];
        $where['brand_id']=$cause['guid'];
        $goods=DB::name('goods')->where($where)->field('g_id')->select()->toArray();
        $goods_id=array();
        foreach($goods as $k=>$v)
        {
            $goods_id[$k]=$v['g_id'];
        }
        $goods_id=implode(',',$goods_id);
        if($goods_id !=null)
        {
            $order=DB::name('order_detail')->where('goods_id','in',$goods_id)->find();
            if($order)
            {
                $status['num']=0;
                $status['con']='类别下有订单，不能删除！';
            }else{
                $goodsclass=DB::name('brand')->where($cause)->delete();
                if($goodsclass)
                {
                    $status['num']=1;
                    $status['con']='删除成功';
                }else{
                    $status['num']=0;
                    $status['con']='删除失败！';
                }
            }
        }else{
            $goodsclass=DB::name('brand')->where($cause)->delete();
            if($goodsclass)
            {
                $status['num']=1;
                $status['con']='删除成功';
            }else{
                $status['num']=0;
                $status['con']='删除失败！';
            }
        }
        return $status;
    }
    /**
     * 根据品牌id获取指定的规格
    */
    public function getgone($array){
        $where['guid']=$array['guid'];
        $where['shop_id']=$array['shop_id'];
        $date=Db::name("brand")->where($where)->find();
        return $date;
    }
}

